package com.atguigu.service.Impl;

import com.atguigu.mapper.PaymentMapper;
import com.atguigu.service.OrderService;
import com.atguigu.service.PaymentService;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class  PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {
    @Autowired
    private OrderService  orderService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //每个支付订单只有一条支付记录
        //根据订单id和支付方法 来查询记录
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper<>();
         queryWrapper.eq("order_id",orderInfo.getId());
         queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        if (paymentInfo!=null){
            return;
        }
        //要给订单记录里面的属性赋值
        PaymentInfo payment =new PaymentInfo();
        payment.setOutTradeNo(orderInfo.getOutTradeNo());
        payment.setOrderId(orderInfo.getId());
        payment.setPaymentType(paymentType);
        //payment.setTradeNo();
        payment.setTotalAmount(orderInfo.getAmount()); //订单金额
        String subject=new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        payment.setSubject(subject);  //设置交易内容
        payment.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());//支付中 或者未支付
        payment.setCreateTime(new Date());
        payment.setUpdateTime(new Date());
        baseMapper.insert(payment);
    }

    @Override
    public void paySuccess(String out_trade_no, Map<String, String> resultMap) {
        //1、根据out_trade_no查询订单
        QueryWrapper<OrderInfo> queryWrapper  =new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",out_trade_no);
        OrderInfo orderInfo = orderService.getOne(queryWrapper);

        //修改订单状态（已经支付）
         orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
         orderService.updateById(orderInfo);
        //3、根据out_trade_no查询支付记录
        QueryWrapper<PaymentInfo> queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("out_trade_no",out_trade_no);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper1);
        //4、修改支付记录状态（已支付）
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));//微信端流水号
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(resultMap.toString());
        baseMapper.updateById(paymentInfo);

    }

    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper  =new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        return paymentInfo;
    }
}
